package org.wxd.bi.data;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wxd.batis.sql.mysql.MysqlDataHelper;
import org.wxd.bi.api.PushApi;
import org.wxd.bi.api.po.count.UserRRData;
import org.wxd.bi.api.po.pushlog.LoginUserLog;
import org.wxd.bi.api.struct.PushMapping;
import org.wxd.bi.po.AppInfo;
import org.wxd.system.ClassUtil;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Troy.Chen(無心道, 15388152619)
 * @version: 2021-09-28 23:23
 **/
public class AppService implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger log = LoggerFactory.getLogger(AppService.class);

    private AppInfo appInfo;
    private MysqlDataHelper appDataHelper;
    private Map<String, PushMapping> dataMappingMap = new LinkedHashMap<>();

    public AppService(AppInfo appInfo, MysqlDataHelper appDataHelper) throws Exception {
        this.appInfo = appInfo;
        this.appDataHelper = appDataHelper;
        this.appDataHelper.createTable(PushMapping.class);

        final Collection<Class<?>> classes = ClassUtil.getClasses(LoginUserLog.class.getPackage().getName(), UserRRData.class.getPackage().getName());
        for (Class<?> loadClass : classes) {
            this.appDataHelper.createTable(loadClass);
            PushMapping pushMapping = PushApi.buildPushMapping(loadClass);
            pushMapping.sortColumn();
            this.appDataHelper.replace(pushMapping);
        }

        final List<PushMapping> mappings = appDataHelper.queryEntities(PushMapping.class);
        for (PushMapping mapping : mappings) {
            dataMappingMap.put(mapping.getTableName(), mapping);
        }
    }

    public AppInfo getAppInfo() {
        return appInfo;
    }

    public MysqlDataHelper getAppDataHelper() {
        return appDataHelper;
    }

    public Map<String, PushMapping> getDataMappingMap() {
        return dataMappingMap;
    }

}
